package com.zhc.dejingapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhc.dejingapi.common.result.Result;
import com.zhc.dejingapi.entity.*;
import com.zhc.dejingapi.entity.query.CodeQuery;
import com.zhc.dejingapi.entity.vo.Code2Vo;
import com.zhc.dejingapi.entity.vo.QingInjectAndSendWorkVo;
import com.zhc.dejingapi.entity.vo.TimeVo;
import com.zhc.dejingapi.mapper.*;
import com.zhc.dejingapi.service.QingInjectreportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-10-10
 */
@Service
public class QingInjectreportServiceImpl extends ServiceImpl<QingInjectreportMapper, QingInjectreportEntity> implements QingInjectreportService {

    @Resource
    private QingInjectreportMapper qingInjectreportMapper;

    @Resource
    private QingFcmoroutingdetailMapper qingFcmoroutingdetailMapper;

    @Resource
    private QingInjectionmoldsendworkdetailMapper qingInjectionmoldsendworkdetailMapper;

    @Resource
    private QingProcessplanMapper qingProcessplanMapper;
    @Resource
    private QingPersonMapper qingPersonMapper;

    @Resource
    private QingBoilingsendworkMapper qingBoilingsendworkMapper;
    @Override
    public Result reportJob(QingInjectAndSendWorkVo qingInjectAndSendWorkVo) {
        QingInjectreportEntity qingInjectreportEntity = new QingInjectreportEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo, qingInjectreportEntity);
        qingInjectreportEntity.getPsnCode();
        QueryWrapper<QingPersonEntity> qWPerson=new QueryWrapper<>();
        qWPerson.eq("psnCode",qingInjectAndSendWorkVo.getPsnCode());
        QingPersonEntity qingPersonEntity = qingPersonMapper.selectOne(qWPerson);
        qingInjectreportEntity.setPsnName(qingPersonEntity.getPsnName());
        int insert = qingInjectreportMapper.insert(qingInjectreportEntity);

        //TODO
        // 箱码
        Date date = DateUtil.date();
        String dateformat = DateUtil.format(date, "yyyyMMdd");
        String processPlanCode = qingInjectAndSendWorkVo.getProcessPlanCode();
        String barCode = processPlanCode +"-"+ dateformat;
        long barCodeNum = qingFcmoroutingdetailMapper.getBarCodeNum(barCode);

            String barcodeStr = "";

            if (barCodeNum < 1) {

                    if(qingInjectreportEntity.getModQty()==0){
                        for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                            QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                            String num = String.format("%03d", i);
                            barcodeStr = barCode +"-"+ num;
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }
                    }else{
                        for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                            QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                            String num = String.format("%03d", i);
                            barcodeStr = barCode +"-"+ num;
                            if(i<qingInjectreportEntity.getProNum()){
                                qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                                qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                                qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                                qingFcmoroutingdetailEntity.setINum(1);
                                qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                            }else {
                                qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                                qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                                qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                                qingFcmoroutingdetailEntity.setINum(1);
                                qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                            }

                        }

                    }



            } else {

                if(qingInjectreportEntity.getModQty()==0){
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", barCodeNum + i);
                        barcodeStr = barCode +"-"+ num;
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }
                }else{
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", barCodeNum + i);
                        barcodeStr = barCode +"-"+ num;
                        if(i<qingInjectreportEntity.getProNum()){
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }else {
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }

                    }

                }

            }


        //TODO
        // 注塑派工单回写报工数量
//        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
//
//        queryWrapper.eq("zs_send_work_code",qingInjectreportEntity.getZsSendWorkCode());
//        QingInjectionmoldsendworkdetailEntity qingInSend = qingInjectionmoldsendworkdetailMapper.selectOne(queryWrapper);
//        int totalReportNum =0;
//        if(qingInSend.getTotalReportNum()!=null){
//            totalReportNum=qingInSend.getTotalReportNum();
//        }
//
//        qingInSend.setTotalReportNum(totalReportNum+qingInjectreportEntity.getQualifyQty());
//        qingInjectionmoldsendworkdetailMapper.update(qingInSend,queryWrapper);

        //TODO
        // 工序计划回写报工数量

        QueryWrapper<QingProcessplanEntity> qW=new QueryWrapper<>();

        qW.eq("process_plan_code",qingInjectreportEntity.getProcessPlanCode());
        QingProcessplanEntity qingProcessplan = qingProcessplanMapper.selectOne(qW);
        int totalReportNum1 =0;
        if(qingProcessplan.getTotalReportNum()!=null){
            totalReportNum1=qingProcessplan.getTotalReportNum();
        }
        qingProcessplan.setTotalReportNum(totalReportNum1+qingInjectreportEntity.getQualifyQty());
        qingProcessplanMapper.update(qingProcessplan,qW);
        if(insert>0){
            return Result.ok(qingInjectreportEntity.getMainId());
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public Result updateReport(QingInjectAndSendWorkVo qingInjectAndSendWorkVo) {
        QingInjectreportEntity qingInjectreportEntity = new QingInjectreportEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo, qingInjectreportEntity);
        QueryWrapper<QingInjectreportEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("main_id",qingInjectreportEntity.getMainId());
        QingInjectreportEntity qingInjectreportEntity1 = qingInjectreportMapper.selectOne(queryWrapper);

        int update = qingInjectreportMapper.update(qingInjectreportEntity, queryWrapper);
        if(!Objects.equals(qingInjectreportEntity1.getProNum(), qingInjectreportEntity.getProNum()) || !Objects.equals(qingInjectreportEntity1.getQtyPerCarton(), qingInjectreportEntity.getQtyPerCarton()) || !Objects.equals(qingInjectreportEntity1.getModQty(), qingInjectreportEntity.getModQty()) || !Objects.equals(qingInjectreportEntity1.getQualifyQty(), qingInjectreportEntity.getQualifyQty())){
            QueryWrapper<QingFcmoroutingdetailEntity> qW=new QueryWrapper<>();
            qW.eq("main_id",qingInjectreportEntity.getMainId());
            qingFcmoroutingdetailMapper.delete(qW);
            Date date = DateUtil.date();
            String dateformat = DateUtil.format(date, "yyyyMMdd");
            String processPlanCode = qingInjectAndSendWorkVo.getProcessPlanCode();
            String barCode = processPlanCode +"-"+ dateformat;
            long barCodeNum = qingFcmoroutingdetailMapper.getBarCodeNum(barCode);

            String barcodeStr = "";

            if (barCodeNum < 1) {

                if(qingInjectreportEntity.getModQty()==0){
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", i);
                        barcodeStr = barCode +"-"+ num;
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }
                }else{
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", i);
                        barcodeStr = barCode +"-"+ num;
                        if(i<qingInjectreportEntity.getProNum()){
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }else {
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }

                    }

                }



            } else {

                if(qingInjectreportEntity.getModQty()==0){
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", barCodeNum + i);
                        barcodeStr = barCode +"-"+ num;
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }
                }else{
                    for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                        QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                        String num = String.format("%03d", barCodeNum + i);
                        barcodeStr = barCode +"-"+ num;
                        if(i<qingInjectreportEntity.getProNum()){
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }else {
                            qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                            qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                            qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                            qingFcmoroutingdetailEntity.setINum(1);
                            qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                        }

                    }

                }

            }


            //TODO
            // 注塑派工单回写报工数量
//        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
//
//        queryWrapper.eq("zs_send_work_code",qingInjectreportEntity.getZsSendWorkCode());
//        QingInjectionmoldsendworkdetailEntity qingInSend = qingInjectionmoldsendworkdetailMapper.selectOne(queryWrapper);
//        int totalReportNum =0;
//        if(qingInSend.getTotalReportNum()!=null){
//            totalReportNum=qingInSend.getTotalReportNum();
//        }
//
//        qingInSend.setTotalReportNum(totalReportNum+qingInjectreportEntity.getQualifyQty());
//        qingInjectionmoldsendworkdetailMapper.update(qingInSend,queryWrapper);

            //TODO
            // 工序计划回写报工数量

            QueryWrapper<QingProcessplanEntity> qWp=new QueryWrapper<>();

            qW.eq("process_plan_code",qingInjectreportEntity.getProcessPlanCode()).eq("process_name","注塑");
            QingProcessplanEntity qingProcessplan = qingProcessplanMapper.selectOne(qWp);
            int totalReportNum1 =0;
            if(qingProcessplan.getTotalReportNum()!=null){
                totalReportNum1=qingProcessplan.getTotalReportNum();
            }
            qingProcessplan.setTotalReportNum(totalReportNum1+qingInjectreportEntity.getQualifyQty());
            qingProcessplanMapper.update(qingProcessplan,qWp);

        }
        if(update>0){
            return Result.ok(qingInjectAndSendWorkVo);
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public Result deleteReport(QingInjectreportEntity qingInjectreport) {
        QueryWrapper<QingInjectreportEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("main_id",qingInjectreport.getMainId());

        QueryWrapper<QingFcmoroutingdetailEntity> qW=new QueryWrapper<>();
        qW.eq("main_id",qingInjectreport.getMainId());
        Long l = qingFcmoroutingdetailMapper.selectCount(qW);
        int delete1 = qingFcmoroutingdetailMapper.delete(qW);
        int i=0;
        if(delete1>0){
            i++;
        }
        if(l==i){
            int delete = qingInjectreportMapper.delete(queryWrapper);
            if(delete>0){
                return Result.ok(null);
            }else {
                return Result.fail(null);
            }
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public PageInfo<QingInjectreportEntity> selectReport(CodeQuery codeQuery) {

        int current=codeQuery.getPageCurrent();
        int size=codeQuery.getPageSize();

        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(current, size);
        QueryWrapper<QingInjectreportEntity> queryWrapper=new QueryWrapper<>();
        if(codeQuery.getReportJobTime()!=null){
            queryWrapper.likeRight("report_time",codeQuery.getReportJobTime());
        }

        //只有紧跟着PageHelper.startPage()的sql起了作用
        List<QingInjectreportEntity> codeList = qingInjectreportMapper.selectList(queryWrapper);
        //用PageInfo对结果进行包装
        PageInfo<QingInjectreportEntity> page = new PageInfo<>(codeList);

        return page;
    }

    @Override
    public PageInfo<Code2Vo> getCodeList(CodeQuery codeQuery) {
        int current=codeQuery.getPageCurrent();
        int size=codeQuery.getPageSize();

        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(current, size);
        //只有紧跟着PageHelper.startPage()的sql起了作用
        List<Code2Vo> codeList = qingInjectreportMapper.getCodeList(codeQuery);
        //用PageInfo对结果进行包装
        PageInfo<Code2Vo> page = new PageInfo<>(codeList);

        return page;
    }

    @Override
    public Result reportBoilJob(QingInjectAndSendWorkVo qingInjectAndSendWorkVo) {
        QueryWrapper<QingProcessplanEntity> qW=new QueryWrapper<>();
        qW.eq("order_code",qingInjectAndSendWorkVo.getOrderCode()).eq("process_name","注塑");
        QingProcessplanEntity qingProcessplan = qingProcessplanMapper.selectOne(qW);
        QingInjectreportEntity qingInjectreportEntity = new QingInjectreportEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo,qingInjectreportEntity);
        qingInjectreportEntity.setMainId(null);
        qingInjectreportEntity.setOrderCode(qingProcessplan.getOrderCode());
        qingInjectreportEntity.setProcessCode(qingProcessplan.getProcessCode());
        qingInjectreportEntity.setEqCode(qingProcessplan.getEqCode());
        qingInjectreportEntity.setEqName(qingProcessplan.getEqName());
        qingInjectreportEntity.setProcessPlanCode(qingProcessplan.getProcessPlanCode());
        qingInjectreportEntity.setQualifyQty(qingInjectAndSendWorkVo.getEachCartonQty());
        qingInjectreportEntity.setProNum(1);
        qingInjectreportEntity.setModQty(0);
        qingInjectreportEntity.setQtyPerCarton(qingInjectAndSendWorkVo.getEachCartonQty());


        int insert = qingInjectreportMapper.insert(qingInjectreportEntity);

        //TODO
        // 箱码
        Date date = DateUtil.date();
        String dateformat = DateUtil.format(date, "yyyyMMdd");
        String processPlanCode = qingInjectreportEntity.getProcessPlanCode();
        String barCode = processPlanCode +"-"+ dateformat;
        long barCodeNum = qingFcmoroutingdetailMapper.getBarCodeNum(barCode);

        String barcodeStr = "";

        if (barCodeNum < 1) {

            if(qingInjectreportEntity.getModQty()==0){
                for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                    QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                    String num = String.format("%03d", i);
                    barcodeStr = barCode +"-"+ num;
                    qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                    qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                    qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                    qingFcmoroutingdetailEntity.setINum(1);
                    qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                }
            }else{
                for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                    QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                    String num = String.format("%03d", i);
                    barcodeStr = barCode +"-"+ num;
                    if(i<qingInjectreportEntity.getProNum()){
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }else {
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }

                }

            }



        } else {

            if(qingInjectreportEntity.getModQty()==0){
                for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                    QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                    String num = String.format("%03d", barCodeNum + i);
                    barcodeStr = barCode +"-"+ num;
                    qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                    qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                    qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                    qingFcmoroutingdetailEntity.setINum(1);
                    qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                }
            }else{
                for(int i=1;i<=qingInjectreportEntity.getProNum();i++) {
                    QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = new QingFcmoroutingdetailEntity();
                    String num = String.format("%03d", barCodeNum + i);
                    barcodeStr = barCode +"-"+ num;
                    if(i<qingInjectreportEntity.getProNum()){
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getQtyPerCarton());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }else {
                        qingFcmoroutingdetailEntity.setMainId(qingInjectreportEntity.getMainId());
                        qingFcmoroutingdetailEntity.setBarcodeStr(barcodeStr);
                        qingFcmoroutingdetailEntity.setIQty(qingInjectreportEntity.getModQty());
                        qingFcmoroutingdetailEntity.setINum(1);
                        qingFcmoroutingdetailMapper.insert(qingFcmoroutingdetailEntity);
                    }

                }

            }

        }


        //TODO
        // 注塑派工单回写报工数量
//        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
//
//        queryWrapper.eq("zs_send_work_code",qingInjectreportEntity.getZsSendWorkCode());
//        QingInjectionmoldsendworkdetailEntity qingInSend = qingInjectionmoldsendworkdetailMapper.selectOne(queryWrapper);
//        int totalReportNum =0;
//        if(qingInSend.getTotalReportNum()!=null){
//            totalReportNum=qingInSend.getTotalReportNum();
//        }
//
//        qingInSend.setTotalReportNum(totalReportNum+qingInjectreportEntity.getQualifyQty());
//        qingInjectionmoldsendworkdetailMapper.update(qingInSend,queryWrapper);

        //TODO
        // 工序计划回写报工数量

        QueryWrapper<QingProcessplanEntity> qW1=new QueryWrapper<>();

        qW1.eq("process_plan_code",qingInjectreportEntity.getProcessPlanCode());
        QingProcessplanEntity qingProcessplan1 = qingProcessplanMapper.selectOne(qW1);
        int totalReportNum1 =0;
        if(qingProcessplan1.getTotalReportNum()!=null){
            totalReportNum1=qingProcessplan1.getTotalReportNum();
        }
        qingProcessplan1.setTotalReportNum(totalReportNum1+qingInjectreportEntity.getQualifyQty());
        qingProcessplanMapper.update(qingProcessplan1,qW1);

        QingBoilingsendworkEntity qingBoilingsendworkEntity = new QingBoilingsendworkEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo,qingBoilingsendworkEntity);
        qingBoilingsendworkEntity.setMainId(null);
        qingBoilingsendworkEntity.setApplyId(null);
        qingBoilingsendworkEntity.setBoilingStatus("启动");
        //水处理启动报工
        int insert1 = qingBoilingsendworkMapper.insert(qingBoilingsendworkEntity);

        if(insert>0){
            return Result.ok(qingInjectreportEntity);
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public Result getReportStartTime(String processPlanCode) {
        QueryWrapper<QingInjectreportEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("process_plan_code",processPlanCode);
        queryWrapper.orderByDesc("report_end_time");
        List<QingInjectreportEntity> qingInjectreportEntities = qingInjectreportMapper.selectList(queryWrapper);
        TimeVo timeVo = new TimeVo();
        if(qingInjectreportEntities.isEmpty()){
            QueryWrapper<QingProcessplanEntity> qW=new QueryWrapper<>();
            qW.eq("process_plan_code",processPlanCode);
            QingProcessplanEntity qingProcessplanEntity = qingProcessplanMapper.selectOne(qW);
            if(qingProcessplanEntity==null){
                return Result.fail("未找到对应工序计划");
            }else{
                timeVo.setReportStartDate(qingProcessplanEntity.getRealStartDate());
                return Result.ok(timeVo);
            }

        }else{
            timeVo.setReportStartDate(qingInjectreportEntities.get(0).getReportEndTime());
            return Result.ok(timeVo);
        }
    }

}
